import * as React from 'react';
import * as Reflux from 'reflux';
import * as $ from 'jquery';

import { IN } from './in';
import { SUB } from './sub';
import * as Validator from '../../../../../../common/util/regex';
import * as add from './add-reflux';





let SupplierCreate = React.createClass({
    getInitialState: function () {
        return {
            isShowDialog:false,
            data: {},
            "name": {
                "name": "name",
                "type": "input",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "供应商名称",
                "errorMessage": false,
                "vaMethod": function () {
                    return true;
                }
            }, //供应商名词
            "type": {
                "ref": "diliverFee",
                "name": "type",
                "type": "select",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "供应商类型",
                "errorMessage": false,
                "keyName": "supplier.type",
                vaMethod: function (value, $in) {
                    var param = $in.props.getState("diliverFee"), temp = {};
                    //console.error("liandong :" + JSON.stringify(value));
                    if (value.id == 1) {
                        param.isShow = false;
                        param.errorMessage = true;
                        temp["diliverFee"] = param;
                        $in.setFormValue({"name": "diliverFee", value: ""});
                        $in.props.changState(temp);
                    } else {
                        param.isShow = true;
                        temp["diliverFee"] = param;
                        $in.props.changState(temp)
                    }
                    return true;
                }
            },  //供应商类型
            "chargeUserName": {
                "name": "chargeUserName",
                "type": "input",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "负责人",
                "errorMessage": false
            },//负责人
            "chargeUserMobile": {
                "name": "chargeUserMobile",
                "type": "input",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "负责人联系电话",
                "errorMessage": false,
                "vaMethod": function (value) {
                    return Validator.isMobile.test(value) ? true : '必须是一个11位的手机号';
                }
            },//chargeUserMobile
            "diliverFee": {
                "name": "diliverFee",
                "type": "input_label",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "配送金额",
                "subTitle": "元/次",
                "errorMessage": false,
                "vaMethod": function (value) {
                    return Validator.isDouble.test(value) ? true : '必须是一个双精度数字';
                }
            },//配送金额
            "diliverUserName": {
                "name": "diliverUserName",
                "type": "input",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "发货人",
                "errorMessage": false
            },//发货人
            "diliverUserMobile": {
                "name": "diliverUserMobile",
                "type": "input",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "发货人联系电话",
                "errorMessage": false,
                "vaMethod": function (value) {
                    return Validator.isMobile.test(value) ? true : '必须是一个11位的手机号';
                }
            }, //发货人联系电话
            "payCycle": {
                "name": "payCycle",
                "type": "inputlabel",
                "isStar": true,
                "csslength": 1,
                "defaultValue": "",
                "title": "付账周期",
                "subTitle": "天",
                "errorMessage": false
            },//付账周期  天
            "contractImgs": {
                "name": "contractImgs",
                "type": "upimage",
                "isStar": true,
                "csslength": 3,
                "defaultValue": "",
                "title": "合同图片",
                "errorMessage": false
            }, // 合同图片  使用“;”分隔
            "remark": {
                "name": "remark",
                "type": "textarea",
                "isStar": false,
                "csslength": 2,
                "defaultValue": "",
                "title": "备注",
                "errorMessage": false
            }  //备注
        };
    },
    onStatusChange: function (data) {
        if (data.resultCode == "1") {
            alert(data.resultMsg)
        } else {
            this.props.history.push({
                pathname: '/supplier/list'
            });
        }
    },
    /* componentWillUpdate:function(){
     if(this.state.dataResult&&this.state.dataResult.resultCode=="0"){
     ////console.log(JSON.st)
     alert("已经提交数据");
     }else if(this.state.dataResult&&this.state.dataResult.resultCode=="1"){
     alert(this.state.dataResult.errorMessage);
     }
     },*/
    setFormValue: function (item) {
        var data = this.state.data;
        data[item.name] = item.value;
        this.setState({
            "data": data,
        });

        //console.log(JSON.stringify(this.state))
    },
    changState: function (value) {
        //console.log("shezhi :" + JSON.stringify(value));
        return this.setState(value);
    },
    getState: function (name) {
        return this.state[name];
    },
    isNotNull: function (name, value, isNotNull) {

        if (isNotNull && value == "") {
            changState(name, "不能为空");
            return false;
        }
        return false;
    },
    handleSubmit: function () {
        var key, state = this.state, temp, newState = {}, isVa = true;

        for (key in state) {
            temp = state[key];
            //console.log("key:" + key + "   errorMessage:" + temp.errorMessage);
            if (temp.title && temp.errorMessage != true && temp.isStar) {
                temp.errorMessage = temp.errorMessage == false ? "不能为空" : temp.errorMessage;
                //console.log("error:" + temp.errorMessage);
                newState[key] = temp;
                isVa = false;
            }
        }
        this.setState(newState);
        //console.log(" this.setState:" + JSON.stringify(newState));
        if (isVa) {

            this.doSubmit(this.state.data);
        }

       /* this.setState({
                "isShowDialog":true
        })*/
    },
    doSubmit: function (hh) {
        var gg, ff, contractImgs = [], i = 0, j;
        gg = clone3(hh);
        for (; i < gg.contractImgs.length; i++) {
            contractImgs.push(gg.contractImgs[i].key)
        }
        gg.contractImgs = contractImgs.join(";");
        gg.type = gg.type.id;
        //console.log("要提交数据:" + contractImgs);
        TodoStore.get("/api/caigou/supplier/create", gg);
        return false;
    },
    render: function () {
        //console.log("55555555" + JSON.stringify(this.state.data));
        return (
            <div className="container-form">
                <div className="form-head">
                    <p className="form-head-title">预申请单明细</p>
                </div>
                <div className="form-in">
                    <div className="form-in-column">
                        <div className="from-row">
                            <IN param={this.state.name} changState={this.changState} getState={this.getState}/>
                            <IN param={this.state.type} changState={this.changState} getState={this.getState}/>
                            <IN param={this.state.diliverFee} changState={this.changState} getState={this.getState}/>
                        </div>
                        <div className="from-row">
                            <IN param={this.state.chargeUserName} changState={this.changState} getState={this.getState}/>
                            <IN param={this.state.chargeUserMobile} changState={this.changState} getState={this.getState}/>
                        </div>

                        <div className="from-row">
                            <IN param={this.state.payCycle} changState={this.changState} getState={this.getState}/>
                        </div>
                        <div className="from-row">
                            <IN param={this.state.diliverUserName} changState={this.changState} getState={this.getState}/>
                            <IN param={this.state.diliverUserMobile} changState={this.changState} getState={this.getState}/>
                        </div>
                        <div className="from-row">
                            <IN param={this.state.remark} changState={this.changState} getState={this.getState}/>
                        </div>
                    </div>
                </div>
                <div className="form-head">
                    <p className="form-head-title">合同</p>
                </div>
                <div className="form-in">
                    <IN param={this.state.contractImgs} changState={this.changState} getState={this.getState}/>
                </div>
                <div className="form-in">
                    <div className="from-row">
                        <div className="from-area">
                            <label className="form-label"></label>
                            <div className="from-in-area">
                                <SUB handleSubmit={this.handleSubmit}/>
                            </div>
                        </div>

                    </div>
                </div>
                <Dialog show={this.state.isShowDialog} title="我也不知道弄啥" >
                </Dialog>
            </div>
        )
    }
});

module.exports = SupplierCreate;
